package com.easycode.devtool.service;



import com.easycode.devtool.domain.TableModel;

import java.util.Arrays;

/**
 * Created by yangyanchen on 2017/2/8.
 */
public final class Jsp2Service {

    public static String generateListJsp(TableModel tableModel) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("<%@ page language=\"java\" contentType=\"text/html;charset=UTF-8\"%>\n")
                .append("<body>\n")
                .append("<template slot='controls' scope='app'>\n")
                .append("<H1>"+tableModel.getTableComment()+"列表</H1>\n")
                .append("</template>")
                .append("<template scope='app'>\n")
                .append("<control-table tablekey='table' :app='app' style='width: 1000px;' :loading='app.controls.table.loading'>\n" +
                        "        <template scope='app' slot='controls'>\n" +
                        "            <el-row>\n")
                .append("<el-col :span='8'>\n");

        tableModel.getFields().stream().forEach(item->{

            if(!Arrays.asList("id","creator_id","create_at","update_at").contains(item.getFieldName().toLowerCase()))
            {
                buffer.append("<control-item type='input' label='"+item.getFieldComment()+"'   :app='app.controls.app' :local='app.controls.table.searchParams' datakey='"+item.getCamelFieldName()+"'></control-item>\n");
            }
        });

        buffer.append("</el-col>\n</el-row>\n" +
                "            <el-row>\n" +
                "                <el-col :span='8'><el-button @click='app.controls.table.doSearch()' type='primary' icon='search'>搜索</el-button></el-col>\n" +
                "            </el-row>");
        buffer.append("  <el-row>\n" +
                "                <el-col :span='8'><el-button @click='app.controls.app.clearField(app.controls.table.add); app.controls.table.dialog.add=true' type='new' icon='plus'  :disabled='!app.controls.app.Perm.write'>新增</el-button></el-col>\n" +
                "            </el-row>\n" +
                "        </template>");


        //caozuo
        buffer.append("<el-table-column label='操作' >\n" +
                "            <template scope='inner'>\n" +
                "                <el-button type='danger' size='mini' @click='app.controls.table.doDelete(inner.row)' :disabled='!app.controls.app.Perm.write'>删除</el-button>\n" +
                "            </template>\n" +
                "        </el-table-column>");

        //field
        tableModel.getFields().stream().forEach(item->{

            if(!Arrays.asList("id","creator_id","create_at","update_at").contains(item.getFieldName().toLowerCase()))
            {
                buffer.append("<el-table-column prop='"+item.getCamelFieldName()+"' label='"+item.getFieldComment()+"'>\n" +
                        "            <template scope='inner'>\n" +
                        "                <control-item :app='app' :local='inner.row' datakey='"+item.getCamelFieldName()+"' type='edit' @saved='app.controls.table.doUpdate(inner.row)' @discard='app.controls.table.refresh()' :item-disabled='!app.controls.app.Perm.write'></control-item>\n" +
                        "            </template>\n" +
                        "        </el-table-column>\n");
            }else
            {
                if("id".equals(item.getFieldName().toLowerCase()))
                {
                    buffer.append("<el-table-column prop='id' label='id' width='50'></el-table-column>");
                }
            }
        });


        buffer.append("</control-table>\n");

        //add panel

        buffer.append("<dialog-create\n" +
                "            :visible='app.controls.table.dialog.add'\n" +
                "            @update:visible='app.controls.table.dialog.add=arguments[0]'\n" +
                "            :title='\"添加\"'\n" +
                "            :app='app.controls.app'\n" +
                "            :controls='app.controls'\n" +
                "            @ok='app.controls.table.doAdd()'>\n" +
                "        <template scope='app'>\n" +
                "        <el-card>\n");

        tableModel.getFields().stream().forEach(item->{

            if(!Arrays.asList("id","creator_id","create_at","update_at").contains(item.getFieldName().toLowerCase()))
            {
                buffer.append("\t<el-row><el-col><control-item :app='app.controls.app' type='input' :local='app.controls.table.add' datakey='"+item.getCamelFieldName()+"' label='"+item.getFieldComment()+"'></control-item></el-col></el-row>\n");
            }
        });


        buffer.append("\t</el-card>\n" +
                "        </template>\n" +
                "    </dialog-create>\n" +
                "</template>\n" +
                "\n" +
                "</body>\n" +
                "<js>\n" +
                "    <script>\n");

        //js
        buffer.append(" var c = window.layoutData.controls;\n" +
                "        c.table = {\n" +
                "            data: [],\n" +
                "            page: 1,\n" +
                "            pageSize: 20,\n" +
                "            total: 0,\n" +
                "            search: {\n" +
                "                start: 0,\n" +
                "            },\n\tsearchParams: {");


        tableModel.getFields().stream().forEach(item-> {
            if(!Arrays.asList("id","creator_id","create_at","update_at").contains(item.getFieldName().toLowerCase()))
            {
                buffer.append("\t"+item.getCamelFieldName()+ " : \"\",\n");
            }
        });

        buffer.append("\n\t},\n" +
                "            add: {");
        tableModel.getFields().stream().forEach(item-> {
            if(!Arrays.asList("id","creator_id","create_at","update_at").contains(item.getFieldName().toLowerCase()))
            {
                buffer.append("\t"+item.getCamelFieldName()+ " : \"\",\n");
            }
        });
        buffer.append(" \t},\n" +
                "            dialog:{\n" +
                "                add: false,\n" +
                "            },\n" +
                "            get(){\n" +
                "                this.search.limit = this.pageSize;\n" +
                "                this.loading = true;\n" +
                "                this.search.start = (this.page - 1) * this.pageSize ;\n");


        buffer.append("\t\tapp.ajax."+tableModel.getTableName()+".find(this.search).then(x => {\n" +
                "                   this.data = x.data.item;\n" +
                "                   this.total = x.data.total;\n" +
                "                   this.loading = false;\n" +
                "            });\n" +
                "            },\n" +
                "            doAdd(){\n" +
                "                if (true) {\n" +
                "                    app.ajax."+tableModel.getTableName()+".insert(this.add).then(_ => {\n" +
                "                        this.get();\n" +
                "                    this.dialog.add = false;\n" +
                "                });\n" +
                "                } else {\n" +
                "                    app.$message({\n" +
                "                        message: '排序请输入正整数',\n" +
                "                        type: 'warning'\n" +
                "                    })\n" +
                "                }\n" +
                "\n" +
                "            },\n" +
                "            doSearch(){\n" +
                "                Object.assign(this.search, this.searchParams);\n" +
                "                this.get();\n" +
                "            },\n" +
                "            doDelete(row){\n" +
                "                app.ajax."+tableModel.getTableName()+".delete(Object.assign({id: row.id}, row)).then( _ => this.get(), _ => this.get() );\n" +
                "            },\n" +
                "            doUpdate(row){\n" +
                "                app.ajax."+tableModel.getTableName()+".update(row).then( _ => this.get(), _ => this.get());\n" +
                "            }\n" +
                "        };\n" +
                "        $(function(){\n" +
                "            app.service['common/pagination/init'](c.table, _ => c.table.get());\n" +
                "            c.table.get();\n" +
                "            app.controls.app.Perm.write=true;\n" +
                "        })\n" +
                "    </script>\n" +
                "</js>");




        return buffer.toString();
    }

}
